package com.hitrust.gs.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import system.base.ProductOutCategory;

import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.SQLLogger;
import com.hitrust.gs.lib.db.GSBaseDAO;
import com.hitrust.gs.lib.db.GSPreparedStatementFactory;
import com.hitrust.gs.util.exception.GSException;

public class GSProductOutCategoryDao extends GSBaseDAO {
	private final String insertSql = " insert into t_product_out_category (" +
	"product_id,"+
	"status,"+
	"out_category,"+
	"create_time,"+
	"create_user,"+
	"create_user_id,"+
	"last_update_time,"+
	"last_update_user,"+
	"last_update_user_id,"+
	"id"+
	") values (?,?,?,?,?,?,?,?,?,(select sys_guid() from dual))";	
	
	private final String updateSql = " update t_product_out_category set " +
	"status=?,"+
	"out_category = ?,"+
	"last_update_time = ?,"+
	"last_update_user = ?,"+
	"last_update_user_id = ?"+
	"where product_Id = ?";
	/**
	 * 根据ID查询产品供应商分类
	 * @param Id
	 * @return
	 * @throws GSException
	 */
	public boolean findProductBidById (String productCode) throws GSException{
		String tSql = " select * from t_product_out_category where product_id  in (select id from t_product where concentricity_code= ? )";
		boolean tFlag = false;
		Object[] object = {productCode};
		try {
			ArrayList tList = executeQuery( tSql, object);
			if(tList.size()>0){
				tFlag=true;
			}
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return tFlag;
	}
	public ArrayList executeQuery( String aSql,
			Object[] aStmntValues) throws SQLException, Exception {
		try {
			iConn = this.getConnetion();
			SQLLogger.log(aSql, aStmntValues);
			iPstmt = iConn.prepareStatement(aSql);

			// 组合Statement条件.
			if (aStmntValues != null) {
				GSPreparedStatementFactory.buildStatement(iPstmt, aStmntValues);
			}

			// 执行
			iRs = iPstmt.executeQuery();

			ArrayList tArrayList = new ArrayList();
			while (iRs.next()) {
				tArrayList.add("1");
			}
			SQLLogger.log("Success");
			// 返回结果数组列表
			return tArrayList;
		} catch (SQLException e) {
			SQLLogger.log("error:" + e.toString());
			pushSQLError(e.toString());
			GSLogWriter.debug(this, e);
			GSLogWriter.debug(this, aSql);
			throw e;
		} finally {
			this.close();
		}
	}
	/**
	 * 新增产品供应商分类,无连接,不做事务用
	 * @param 实体参数
	 * @return
	 * @throws GSException
	 */
	public boolean insert (ProductOutCategory vo,Connection conn) throws GSException{
		return doExcute(vo,  this.insertSql,conn);
	}
	public boolean doExcute(ProductOutCategory vo,String tSql, Connection conn)
			throws GSException {
		Object[] tobj = new Object[9];
        tobj[0] = vo.getProductId();
        tobj[1] = vo.getStatus();
        tobj[2] = vo.getOutCategory();
        tobj[3] = vo.getCreateTime();
        tobj[4] = vo.getCreateUser();
        tobj[5] = vo.getCreateUserId();
        tobj[6] = vo.getM_last_update_time();
        tobj[7] = vo.getM_last_update_user();
        tobj[8] = vo.getM_last_update_user_id();
		try {
			return this.executeUpdate(conn,tSql, tobj);
		} catch (Exception e) {
			throw new GSException(GSException.E20042,GSException.E20042_MSG);
		}
	};
	/**
	 * 新增产品供应商分类,无连接,不做事务用
	 * @param 实体参数
	 * @return
	 * @throws GSException
	 */
	public boolean update (ProductOutCategory vo,Connection conn) throws GSException{
		String tSql = this.updateSql;
		Object[] tobj = new Object[6];

        tobj[0] = vo.getStatus();
        tobj[1] = vo.getOutCategory();
        tobj[2] = vo.getM_last_update_time();
        tobj[3] = vo.getM_last_update_user();
        tobj[4] = vo.getM_last_update_user_id();
        tobj[5] = vo.getProductId();
		try {
			return this.executeUpdate(conn,tSql, tobj);
		} catch (Exception e) {
			throw new GSException(GSException.E20042,GSException.E20042_MSG);
		}
	};
	
}
